window.onload = function () {
	//游戏页
		var viewWidth = document.documentElement.clientWidth ;
		var oSnowBox = $(".gamebg_box")[0];
		var timerDown = null ;
		var timerSnow = null ;
		var timerMove = null;
		var n = 0; 
		var g = 100 ;
		var w1 = 100;  
		var iCount = 0; 
		var iTime = 30;
		var z = 0;
		var _body = $("body");
		


		// 游戏开始
		var goBtn = $(".go_btn");
		var goBtnWrap = $(".go_btn_wrap");

		goBtn.on('touchend', function () {
		    stopMove()
		})
		function stopMove() {
		    _body.on('touchstart', function (event) {
		        event.preventDefault();
		    })
		}

		goBtn.click(function(){
			$(".pao1").css({"opacity":"1"});
			setTimeout(function(){
				$(".pao1").css({"opacity":"0"});
			},100)

			var boomBgm = $(".boom")[0];

			boomBgm.currentTime = 0;
			boomBgm.play();

			ghostStart();

			setTimeout(function(){
				goBtn.fadeOut();
				goBtnWrap.fadeOut();
			},300)
		})

		
     
		function ghostStart(){
			timerSnow = setInterval(function(){
				z = Math.round(Math.random()*3)+1;
				oSnowBox.innerHTML += '<li style="background-image:url(images/' + z + '.png);background-size:37px auto;"><span></span></li>';
				oSnowBox.innerHTML += '<li style="background-image:url(images/' + 1 + '.png);background-size:60px auto;"><span></span></li>';

				var wNow = Math.round(Math.random()*100);
				if( wNow <50 ){
					wNow = 50;
				}

				var lNow = Math.round(Math.random()*viewWidth) ;
				var aSnow = oSnowBox.getElementsByTagName("li");
				aSnow[n].style.width = 2*wNow+"px" ; //随机宽度
				aSnow[n].style.height = wNow+"px"; //随机高度
				aSnow[n].style.left = lNow+"px";
				n++ ;

				if( n ==100){
					clearInterval(timerSnow);

					//倒计时
					timerDown = setInterval(function(){
						var oTime = document.getElementById("time");

						iTime--;
						if( iTime <10 ){
							iTime = "0"+iTime ;
						}
						oTime.innerHTML = iTime ;
						if( iTime == "00"){
							clearInterval( timerDown );
							clearInterval( timerMove );

							alert("游戏结束");

							// totalP.innerHTML = iCount ;
							// startMove( gameOver ,{ top : 40 , opacity : 100} );
						}
						// console.log(iTime)
						// oTime.innerHTML = iTime ;
					},1000)


					getChest();//获得宝箱
					function getChest(){
						var chestBox =  $(".chest_box");

						chestBox.css({"display":"block"})

						setTimeout(function(){
							chestBox.css({"left":"260px","top":"8px","transition":"1.5s"});
						},1000)
					}


					//图片运动
					var aSnow = oSnowBox.getElementsByTagName("li");
					var aKnife = oSnowBox.getElementsByTagName("span");
					var m =0 ;

					timerMove = setInterval(function(){
						var disL = Math.round(Math.random()*200-180);
						// snowMove( aSnow[m] , { top :oSnowBox.offsetHeight+100 , left : aSnow[m].offsetLeft+disL } );

						// console.log(aSnow[m].offsetLeft)
						aSnow[m].style.top = oSnowBox.offsetHeight+100+"px";
						aSnow[m].style.left = aSnow[m].offsetLeft+disL+"px";

						m++;
						if( m ==130){
							clearInterval(timerMove);
						}
					},500);

					for( var i=0; i<aSnow.length ; i++){
						aSnow[i].boole = 0 ;
						aSnow[i].index = i;

						var oCount = $(".score_point")[0];
						var fireblock = $(".pao_wrap");
						var fire = $(".pao1");
						var boomBgm = $(".boom")[0];


						function playBoom(){
							boomBgm.currentTime = 0;
							boomBgm.play();

							// var boomBgm = document.createElement('audio');
							// boomBgm.src="boom.mp3";
							// boomBgm.oncanplay = function(){
							// 	boomBgm.play();
							// }
							// boomBgm.onpause = function(){
							// 	boomBgm=null;
							// }
						}
						
						aSnow[i].addEventListener('touchstart', function(event) {
							if( this.boole ==0 ){
								this.boole = 1 ;
								var int = Math.round(Math.random() * 20);
								clearInterval(this.timer);
								$(this).find("span").css({"display":"block"})

								fire.css({"opacity":"1"});
								setTimeout(function(){
									fire.css({"opacity":"0"});
								},100)

								playBoom();

							    this.style.background = "block";
								this.className = "active";
								setTimeout(function () {
								   $(this).css({"display":"none"})
								}, 2000)
								iCount += int;
								oCount.innerHTML = iCount;
							    // totalP.innerHTML = iCount;

							    // alert(oCount)

							    // alert(int)
							    var int = Math.round(Math.random() * 100);
							    // var Score = parseInt(datas[int].Score);

							    touch = event.targetTouches[0];
							    lineX = Number(touch.pageX);

							    // alert(lineX)
							    if(lineX<103){
							    	fireblock.css({"transform":"rotate(-25deg)"})
							    }else if(lineX>103 && lineX<206){
							    	fireblock.css({"transform":"rotate(0deg)"})
							    }else if(lineX>206 && lineX<320){
									fireblock.css({"transform":"rotate(25deg)"})
							    }


							    clearInterval(this.timer);
							    // this.innerHTML = Score;
							    this.style.background ="none";
							    //aKnife[this.index].style.display = "block";
							    $(this).find("span").css({"display":"block"})
							    //this.innerHTML = int ;
							    this.className = "active" ;

							    setTimeout(function(){
							    	$(this).css({"display":"none"})
							    },1000)

							    // iCount += Score;
							    oCount.innerHTML = iCount;
							   
							}
						}, false);

						// aSnow[i].addEventListener('touchend', function(event) {
						// 	aSnow[i].removeEventListener( 'touchstart', false);
						// }, false);
					}
				}
			},10);
		}


		//图片运动框架
		function snowMove( obj, json ,fnEnd ){
			clearInterval ( obj.timer );
			obj.timer =setInterval(function(){
				var bStop = true;
				for (var attr in json ){
					var cur = parseInt(getStyle( obj , attr));
					var speed = (json[attr]-cur)/60 ;
					speed=speed>0?Math.ceil(speed):Math.floor(speed);
					if( cur!=json[attr] ){  //若果有cur ！= json[attr] (目标值) 
						bStop = false ;
						obj.style[attr] = speed+cur+"px" ;	
					}
				}
				if( bStop ){ //如果所有的cur 都等于 json[attr] ;
					clearInterval( obj.timer );
					if(fnEnd)fnEnd();
				}
				setTimeout(function(){
					obj.style.display = "none" ;
					clearInterval( obj.timer );
				},3000)
			},30);
		}
	
	
}


//完美运动框架
function startMove( obj , json ,fnEnd ){
	clearInterval( obj.timer );
	obj.timer = setInterval(function(){
		var bStop = true; 
		for( var attr in json ){ //循环json里面的数值 
			var cur = 0;
			if( attr == "opacity" ){
				cur = Math.round(parseFloat(getStyle( obj , "opacity"))*100) ;	
			}else{ 
				cur = parseInt(getStyle( obj , attr));
			}
			var speed = (json[attr]-cur)/5 ;
			
			speed=speed>0?Math.ceil(speed):Math.floor(speed);
			if( cur!=json[attr] ){  //若果有cur ！= json[attr] (目标值) 
				bStop = false ;
				if ( attr == "opacity" ){
					obj.style["filter"] = "alpha(opacity:"+(speed+cur)+")" ;
					obj.style["opacity"] = (speed+cur)/100 ;
				}else{
					obj.style[attr] = speed+cur+"px" ;	
				}
			 }
		}
		if( bStop ){ //如果所有的cur 都等于 json[attr] ;
			clearInterval( obj.timer );
			if(fnEnd)fnEnd();
		}
	},30)
}


//获取行间样式
function getStyle( obj , attr ){
	if( obj.currentStyle ){
		return obj.currentStyle[attr];
	}else{
		return getComputedStyle( obj ,false )[attr];
	}
}